#include "StdAfx.h"
#include "configdialog.h"
#include "qextserialport.h"


ConfigDialog::ConfigDialog(QWidget * parent, Qt::WindowFlags f)
	: QDialog(parent, f)
{
	ui.setupUi(this);
	init();

	connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(slotValidateConfiguration()));
	connect(ui.buttonBox, SIGNAL(accepted()), parent, SLOT(slotOnCloseConfigDialog()));

	ui.comboBoxBaudRate->addItem("110", BAUD110);
    ui.comboBoxBaudRate->addItem("300", BAUD300);
    ui.comboBoxBaudRate->addItem("600", BAUD600);
    ui.comboBoxBaudRate->addItem("1200", BAUD1200);
    ui.comboBoxBaudRate->addItem("2400", BAUD2400);
    ui.comboBoxBaudRate->addItem("4800", BAUD4800);
    ui.comboBoxBaudRate->addItem("9600", BAUD9600);
    ui.comboBoxBaudRate->addItem("19200", BAUD19200);
    ui.comboBoxBaudRate->addItem("38400", BAUD38400);
    ui.comboBoxBaudRate->addItem("57600", BAUD57600);
    ui.comboBoxBaudRate->addItem("115200", BAUD115200);
	ui.comboBoxBaudRate->setCurrentIndex(ui.comboBoxBaudRate->findData(settings->value("BaudRate", BAUD9600))); //9600

    ui.comboBoxDataBits->addItem("5", DATA_5);
    ui.comboBoxDataBits->addItem("6", DATA_6);
    ui.comboBoxDataBits->addItem("7", DATA_7);
    ui.comboBoxDataBits->addItem("8", DATA_8);
	ui.comboBoxDataBits->setCurrentIndex(ui.comboBoxDataBits->findData(settings->value("DataBits", DATA_8))); //8 bit

    ui.comboBoxParity->addItem("None", PAR_NONE);
    ui.comboBoxParity->addItem("Even", PAR_EVEN); //pair
    ui.comboBoxParity->addItem("Odd", PAR_ODD); //impair
    ui.comboBoxParity->addItem("Space", PAR_SPACE);
	ui.comboBoxParity->setCurrentIndex(ui.comboBoxParity->findData(settings->value("Parity", PAR_NONE))); //Parity none

    ui.comboBoxStopBits->addItem("1", STOP_1);
    ui.comboBoxStopBits->addItem("2", STOP_2);
	ui.comboBoxStopBits->setCurrentIndex(ui.comboBoxStopBits->findData(settings->value("StopBits", STOP_1))); //1 stop bit

    ui.comboBoxFlowControl->addItem("None", FLOW_OFF);
    ui.comboBoxFlowControl->addItem("Xon/Xoff", FLOW_XONXOFF);
    ui.comboBoxFlowControl->addItem("Hardware", FLOW_HARDWARE);
	ui.comboBoxFlowControl->setCurrentIndex(ui.comboBoxFlowControl->findData(settings->value("FlowControl", FLOW_OFF))); //Flow control none

	/*Codecs list*/
    foreach (int mib, QTextCodec::availableMibs())
    {
        QTextCodec *codec = QTextCodec::codecForMib(mib);
		ui.comboBoxCodecs->addItem(codec->name(), codec->mibEnum());
    }

	ui.comboBoxCodecs->setCurrentIndex(ui.comboBoxCodecs->findText(settings->value("Codecs", "System").toString())); //By default, codec used is the codec of the system.

	ui.spinBoxTimeout->setValue(settings->value("Timeout", -1).toInt());
}

void ConfigDialog::init()
{
	settings = new QSettings("mbot", "serialConfig");
}

ConfigDialog::~ConfigDialog()
{

}

void ConfigDialog::slotValidateConfiguration()
{
	qDebug() << "slotValidateConfiguration";
	QVariant temp;
	unsigned int index;

	index = ui.comboBoxBaudRate->currentIndex();
	temp = ui.comboBoxBaudRate->itemData(index);
	portSettings.BaudRate = (BaudRateType)temp.toInt();
	settings->setValue("BaudRate", temp.toInt());

	index = ui.comboBoxDataBits->currentIndex();
	temp = ui.comboBoxDataBits->itemData(index);
	portSettings.DataBits = (DataBitsType)temp.toInt();
	settings->setValue("DataBits", temp.toInt());

	index = ui.comboBoxParity->currentIndex();
	temp = ui.comboBoxParity->itemData(index);
	portSettings.Parity = (ParityType)temp.toInt();
	settings->setValue("Parity", temp.toInt());

	index = ui.comboBoxStopBits->currentIndex();
	temp = ui.comboBoxStopBits->itemData(index);
	portSettings.StopBits = (StopBitsType)temp.toInt();
	settings->setValue("StopBits", temp.toInt());

	index = ui.comboBoxFlowControl->currentIndex();
	temp = ui.comboBoxFlowControl->itemData(index);
	portSettings.FlowControl = (FlowType)temp.toInt();
	settings->setValue("FlowControl", temp.toInt());
	
	index = ui.comboBoxCodecs->currentIndex();
	settings->setValue("Codecs", ui.comboBoxCodecs->itemText(index));

	portSettings.Timeout_Millisec = ui.spinBoxTimeout->value();
	settings->setValue("Timeout", ui.spinBoxTimeout->value());

}

PortSettings ConfigDialog::getPortSettings()
{
	QVariant temp;
	temp = settings->value("BaudRate", BAUD9600);
	portSettings.BaudRate = (BaudRateType)temp.toInt();

	temp = settings->value("DataBits", DATA_8);
	portSettings.DataBits = (DataBitsType)temp.toInt();

	temp = settings->value("Parity", PAR_NONE);
	portSettings.Parity = (ParityType)temp.toInt();

	temp = settings->value("StopBits", STOP_1);
	portSettings.StopBits = (StopBitsType)temp.toInt();

	temp = settings->value("FlowControl", FLOW_OFF);
	portSettings.FlowControl = (FlowType)temp.toInt();
	
	temp = settings->value("Timeout", -1);
	portSettings.Timeout_Millisec = temp.toInt();

	return portSettings;
}

QTextCodec* ConfigDialog::getCodecs() const
{
	QVariant temp = settings->value("Codecs");
	QTextCodec* codec = QTextCodec::codecForName(temp.toByteArray());
	return codec;
}